home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / RISK / Source / Simulator.m < prev   
Text File  |  1995-06-12  |  3KB  |  164 lines

  1.  
  2. #import "Simulator.h"
  3. #import "RanGen.h"
  4. #import <appkit/appkit.h>
  5. #import <appkit/Application.h>
  6.  
  7. @implementation Simulator
  8.  
  9. + new
  10. {
  11.   self = [super new];
  12.     theRanGen = [RanGen new];
  13.     
  14.     monthNr = 0;
  15.     capital = 5000.0;
  16.     inflationLoss = 0;
  17.     inflation = 0.31;
  18.     profit = 0;
  19.     profitSum = 0;
  20.     meanProfit = 0;
  21.     percentProfitSum = 0;
  22.     meanPercentProfit = 0;
  23.     return self;
  24. }
  25.  
  26.  
  27. - out:sender
  28. {
  29.   int i;
  30.   actionNumber = 0;
  31.     for (i=1; i<=4; i++)
  32.     {
  33.       if (eval[i] > eval[actionNumber]) actionNumber = i;
  34.     }
  35.     [self month];
  36.     return self;
  37. }
  38.  
  39.  
  40. - setRandomValues
  41. {
  42.   int i,min,max;
  43.     double r1,r2,r3;
  44.     
  45.     r1 = [theRanGen lowInt:20 highInt:40] / 100.0;
  46.     r2 = [theRanGen lowInt:20 highInt:40] / 100.0;
  47.     r3 = [theRanGen lowInt:20 highInt:40] / 100.0;
  48.     inflation = (r1 + r2 + r3) / 3; // 0.2% to 0.4% per month
  49.     
  50.   for (i=0; i<5; i++)
  51.     {
  52.       min = (int)(0.05*capital + 0.5);
  53.       max = (int)(0.20*capital + 0.5);
  54.       cost[i] = [theRanGen lowInt:(min*100) highInt:(max*100)] / 100.0;
  55.       probability[i] = [theRanGen lowInt:670 highInt:995] / 10.0; // %
  56.         r1 = (double)([theRanGen lowInt:170 highInt:250]) / 200.0; //0.9 to 1.1
  57.         expProfit[i] = r1*cost[i];
  58.         eval[i] = (probability[i]/100)*expProfit[i] - cost[i];
  59.  
  60.       [costForm setDoubleValue:cost[i] at:i];
  61.       [succProbForm setDoubleValue:probability[i] at:i];
  62.       [expProfitForm setDoubleValue:expProfit[i] at:i];
  63.       [evalForm setDoubleValue:eval[i] at:i];
  64.     };
  65.     return self;
  66. }
  67.  
  68.  
  69. - aPressed:sender
  70. {
  71.   actionNumber = 0;
  72.     [self month];
  73.   return self;
  74. }
  75.  
  76.  
  77. - bPressed:sender
  78. {
  79.   actionNumber = 1;
  80.     [self month];
  81.   return self;
  82. }
  83.  
  84.  
  85. - cPressed:sender
  86. {
  87.   actionNumber = 2;
  88.     [self month];
  89.   return self;
  90. }
  91.  
  92.  
  93. - dPressed:sender
  94. {
  95.   actionNumber = 3;
  96.     [self month];
  97.   return self;
  98. }
  99.  
  100.  
  101. - ePressed:sender
  102. {
  103.   actionNumber = 4;
  104.     [self month];
  105.   return self;
  106. }
  107.  
  108.  
  109. - month
  110. {
  111.   double oldCapital, percentProfit, r;
  112.     
  113.   chosenCost = cost[actionNumber];
  114.     chosenProbab = probability[actionNumber];
  115.     chosenProfit = expProfit[actionNumber];
  116.     oldCapital = capital;
  117.     
  118.     // inflation loss:
  119.     capital -= chosenCost; // the invested money
  120.     inflationLoss = capital * (inflation / 100.0);
  121.     capital -= inflationLoss;
  122.     
  123.     // profit:
  124.     r = [theRanGen ran01];
  125.     if (r <= chosenProbab/100.0) // success!
  126.     {
  127.       capital += chosenProfit; // the return
  128.         profit = chosenProfit - chosenCost; // the true profit
  129.     }
  130.     else // no success:
  131.     {
  132.       profit = -chosenCost;
  133.     };
  134.     percentProfit = (profit / oldCapital) * 100;
  135.     percentProfitSum += percentProfit;
  136.   profitSum += profit;
  137.     monthNr++;
  138.     meanProfit = profitSum / monthNr;
  139.     meanPercentProfit = percentProfitSum / monthNr;
  140.     
  141.     [self output];
  142.     return self;
  143. }
  144.  
  145.  
  146. - output
  147. {
  148.     // output of last month`s result:
  149.     [monthNrField setIntValue:monthNr];
  150.     [inflationField setDoubleValue:inflation];
  151.     [inflationLossField setDoubleValue:inflationLoss];
  152.     [profitField setDoubleValue:profit];
  153.     [capitalField setDoubleValue:capital];
  154.     [meanProfitField setDoubleValue:meanProfit];
  155.     [meanPercentProfitField setDoubleValue:meanPercentProfit];
  156.     
  157.     // new actions:
  158.     [self setRandomValues];
  159.     
  160.     return self;
  161. }
  162.  
  163.  
  164. @end